
#include "Channels.h"
#include "ChannelManager.h"
#include "Resource.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "WorldPacket.h"
#include "Towns.h"
#include "Effects.h"
#include "EffectManager.h"
#include "EffectModels.h"
#include "EffectModelManager.h"
#include "Users.h"
#include "UserManager.h"
#include "XMLData.h"
#include "Towns.h"
#include "Message.h"
#include "XMLData.h"
#include "OpcodesEx.h"
#include "VipCardManager.h"
#include "Tools.h"
#include "WSSocket.h"


#include "twmjn/TWMJOpcode.h"
#include "twmjn/TWMJAIInterface.h"
#include "twmjn/TWMJGameStruct.h"
#include "twmjn/TWMJXmlDataManager.h"
#include "twmjn/TWMJScriptWorldProcess.h"

#include "GameDefine.h"
#include LOGIC_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include XMLDATA_MGR_PATH
#include LOGIC_MGR_PATH

// 请求玩家信息 0x1000
bool  script_twmj_chr(ResourcePtr & sock,WorldPacket & packet)
{
	WSSocketPtr socket=sock;
	char cCode[14];
	if(!ChkSocketPacket(socket,packet,cCode))
		return true;
	CharPtr chr = socket->getCurrentCharacter();
	if(chr.isNull())
	{
		return true;
	}
	
	if(sProtocolsMgr.CreateCharInforPacket(&packet,chr))
		socket->SendPacket(&packet);
	
	return true;
}

// 请求目标玩家信息 0x1001
bool  script_twmj_targetchr(ResourcePtr & sock,WorldPacket & packet)
{
	WSSocketPtr socket=sock;
	char cCode[14];
	if(!ChkSocketPacket(socket,packet,cCode))
		return true;
	
	uint32 serial;
	packet>>serial;
	
	//Log.Debug("0x0101","serial:%d",serial);
	
	CharPtr chr = sCharMgr.getByHandle(serial);
	if(chr.isNull())
	{
		return true;
	}
	
	if(sProtocolsMgr.CreateTargetCharInforPacket(&packet,chr))
		socket->SendPacket(&packet);
	
	return true;
}

// CMSG_ENTERROOM 0x0011 玩家进入房间
bool  script_twmj_join(ResourcePtr & sock,WorldPacket & packet)
{
	WSSocketPtr socket=sock;
	char cCode[14];
	if(!ChkSocketPacket(socket,packet,cCode))
		return true;
	//Log.Debug("0x0102","1");
	CharPtr chr = socket->getCurrentCharacter();
	if(chr.isNull())
	{
		return true;
	}
	TWMJAICreatureInterface* pChrAiInfe = TO_TWMJCREATURE_INTERFACE(chr->getAIInterface());
	
	if(!pChrAiInfe)
	{
		return true;
	}
	ChannelPtr pChan = pChrAiInfe->GetChannelPtr();
	TownPtr pTown = pChrAiInfe->GetTown();
	if(!pChan.isNull())
	{
		// // 通知直接去牌桌
		// if (sProtocolsMgr.CreateGoHallPacket2(&packet, pTown->getHandle()))
			// socket->SendPacket(&packet);
		// 进入房间
	//	Log.Debug("0x0102","join again?");
		uint32 ai = pChan->getUInt32Field("ai");
		
		{
			TWMJAIChannelInterface *pAiTownInfe = TO_TWMJCHANNEL_INTERFACE(pChan->getAIInterface());
			if (!pAiTownInfe->JoinChannel(chr))
			{
				// 如果失败，进入大厅
				if (sProtocolsMgr.CreateGoHallPacket(&packet))
					socket->SendPacket(&packet);
				pChrAiInfe->SetChannelNull();
				pChrAiInfe->SetTownNull();
			}
		}
		
		return true;
	}
	// if(!pTown.isNull())
	// {
		// Log.Debug("0x0102","ji xu");
		// TWMJAITownInterface* aitownInter = TO_TWMJTOWN_INTERFACE(pTown->getAIInterface());
		// if(!aitownInter)
		// {
			// if(sProtocolsMgr.CreateGoHallPacket2(&packet))
			// socket->SendPacket(&packet);
			// return true;
		// }
		// Log.Debug("0x0102","ji xu 2");
		// uint16 r_r = aitownInter->joinTown(chr);
		
		// return true;
	// }
	//Log.Debug("0x0102","2");
	uint8 townId ;
	
	packet>>townId;
	
	TownPtr tPtr = sTownMgr.getByHandle(uint32(townId));
	
	if(tPtr.isNull())
	{
		if(sProtocolsMgr.CreateGoHallPacket(&packet))
		socket->SendPacket(&packet);
		return true;
	}
	//Log.Debug("0x0102","3");
	TWMJAITownInterface* aitownInter = TO_TWMJTOWN_INTERFACE(tPtr->getAIInterface());
	
	if(!aitownInter)
	{
		if(sProtocolsMgr.CreateGoHallPacket(&packet))
		socket->SendPacket(&packet);
		return true;
	}
	
	uint16 r_r = aitownInter->joinTown(chr);
	//Log.Debug("0x0102","4");
	if(r_r == 0)
	{
		if(sProtocolsMgr.CreateGoHallPacket(&packet,townId))
		socket->SendPacket(&packet);
	}
	else 
	{
		// if(sProtocolsMgr.CreateGoHallPacket2(&packet))
		// socket->SendPacket(&packet);
	}
	
		
	return true;
}

//CMSS_twmj_Ready = 0x0104,		//玩家准备
bool  script_twmj_Ready(ResourcePtr & sock, WorldPacket & packet)
{
	WSSocketPtr socket = sock;
	char cCode[14];
	if (!ChkSocketPacket(socket, packet, cCode))
		return true;
	//Log.Debug("0x0102","1");
	CharPtr chr = socket->getCurrentCharacter();
	if (chr.isNull())
	{
		return true;
	}
	TWMJAICreatureInterface* pChrAiInfe = TO_TWMJCREATURE_INTERFACE(chr->getAIInterface());

	if (!pChrAiInfe)
	{
		return true;
	}
	ChannelPtr pChan = pChrAiInfe->GetChannelPtr();
	TownPtr pTown = pChrAiInfe->GetTown();
	if (pTown.isNull())
	{
		if (sProtocolsMgr.CreateGoHallPacket(&packet))
			socket->SendPacket(&packet);
		return true;
	}

	TWMJAITownInterface* aitownInter = TO_TWMJTOWN_INTERFACE(pTown->getAIInterface());

	if (!aitownInter)
	{
		if (sProtocolsMgr.CreateGoHallPacket(&packet))
			socket->SendPacket(&packet);
		return true;
	}
	if (!pChan.isNull())
	{
		
		uint32 ai = pChan->getUInt32Field("ai");
		
		{
			TWMJAIChannelInterface *pAiTownInfe = TO_TWMJCHANNEL_INTERFACE(pChan->getAIInterface());
			if (!pAiTownInfe->JoinChannel(chr))
			{
				// 如果失败，进入大厅
				if (sProtocolsMgr.CreateGoHallPacket(&packet))
					socket->SendPacket(&packet);
				pChrAiInfe->SetChannelNull();
				pChrAiInfe->SetTownNull();
			}
		}

		return true;
	}
	
	pChan = aitownInter->findChannel();

	if (pChan.isNull())
	{
		if (sProtocolsMgr.CreateGoHallPacket(&packet))
			socket->SendPacket(&packet);
		return true;
	}
	else
	{
		uint32 ai = pTown->getUInt32Field("ai");
		
	
		{
			TWMJAIChannelInterface *pAiTownInfe = TO_TWMJCHANNEL_INTERFACE(pChan->getAIInterface());
			if (!pAiTownInfe->JoinChannel(chr))
			{
				// 如果失败，进入大厅
				if (sProtocolsMgr.CreateGoHallPacket(&packet))
					socket->SendPacket(&packet);
				pChrAiInfe->SetChannelNull();
				pChrAiInfe->SetTownNull();
			}
			else
			{
				if (sProtocolsMgr.CreatePlayerReadyPacket(&packet,0))
					socket->SendPacket(&packet);
			}
		}

	}

	return true;
}

// 玩家离开牌桌 0x0018
bool  script_twmj_leave(ResourcePtr & sock,WorldPacket & packet)
{
	WSSocketPtr socket=sock;
	char cCode[14];
	if(!ChkSocketPacket(socket,packet,cCode))
		return true;
	//Log.Debug("0x0018","1");
	CharPtr chr = socket->getCurrentCharacter();
	if(chr.isNull())
	{
		return true;
	}
	//Log.Debug("0x0018","2");
	AICreatureBaseInterface* aichrInter = TO_CREATURE_BASE_INTERFACE(chr->getAIInterface());
	if(!aichrInter)
	{
		return true;
	}
	Log.Debug("0x0018","3");
	ChannelPtr chnPtr = aichrInter->GetChannelPtr();
	
	if(chnPtr.isNull())
	{
		aichrInter->SetTownNull();
		aichrInter->SetChannelNull();
		chr->setStatus(CharacterStatusFree);
		if (sProtocolsMgr.CreateGoHallPacket(&packet))
			socket->SendPacket(&packet);
		return true;
	}

	

		{
		AIMaJiangChannelBaseInterface* aichanInter = TO_MJCHANNEL_INTERFACE(chnPtr->getAIInterface());

			if (!aichanInter)
			{
				if (sProtocolsMgr.CreateGoHallPacket(&packet))
					socket->SendPacket(&packet);
				return true;
			}

			aichanInter->ExitChannel(chr);
		}
		
	
	
	
	if(sProtocolsMgr.CreateGoHallPacket(&packet))
		socket->SendPacket(&packet);
	//Log.Debug("0x0018","6");
	return true;
}



//CMSS_twmj_goHall = 0x0111,	//客服端准备完成
bool  script_twmj_ready_game(ResourcePtr & sock,WorldPacket & packet)
{
	//Log.Debug("0x0111","0");
	WSSocketPtr socket=sock;
	char cCode[14];
	if(!ChkSocketPacket(socket,packet,cCode))
		return true;
	//Log.Debug("0x0111","1 ");
	uint32 waittime =  sGLMgr.get_acgWaitTime();
	
	if(waittime > 60)
	{
		waittime = 60;
	}
	waittime = sXmlDataMgr.getTWMJconf("STARTWAITTIME");
	
	CharPtr chr = socket->getCurrentCharacter();
	
	if(chr.isNull())
	{
		return true;
	}
	TWMJAICreatureInterface* pChrAiInfe = TO_TWMJCREATURE_INTERFACE(chr->getAIInterface());
		
	
	//Log.Debug("0x0111","zhun bei hao le ");
	if(pChrAiInfe)
	{
		TownPtr oldTown = pChrAiInfe->GetTown();
		ChannelPtr pChan = pChrAiInfe->GetChannelPtr();
		if(!pChan.isNull())
		{
			return true;
		}
		if(oldTown.isNull())
		{
			return true;
		}
		TWMJAITownInterface* aiTownInter = TO_TWMJTOWN_INTERFACE(oldTown->getAIInterface());
		
		if(!aiTownInter)
		{
			return true;
		}
		
	
		
	}
	
	//Log.Debug("0x0111","wait time %d",waittime);
	
	
	return true;
}




